package com.css.fxfzdpzs.modules.fxpg.repository.impl;

import com.css.fxfzdpzs.modules.fxpg.entity.*;
import com.css.fxfzdpzs.modules.fxpg.repository.FxpgRepository;
import com.css.fxfzdpzs.modules.xczh.entity.first.DisasterVisitsCount;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Repository
public class FxpgRepositoryImpl implements FxpgRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public void save(ArrayList<ActivityFaultData> list) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(list.toArray());
        String sql = "INSERT INTO  fxpg_activity_fault_data  " +
                "(id,finished) " +
                "values" +
                "(:id,:finished)";

        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public List<DisasterData> getDisasterBodyData() {
        String sql = "SELECT * FROM fxpg_disaster_data";
        List<DisasterData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisasterData.class));
        return list;
    }

    @Override
    public List<RiskMapData> getMapRiskData(String type) {
        String sql = "SELECT * FROM fxpg_map_risk_data where type = '"+type+"'";
        List<RiskMapData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RiskMapData.class));
        return list;
    }

    @Override
    public void delDisasterData() {
        jdbcTemplate.update("delete  from fxpg_disaster_data");
    }

    @Override
    public void delDisasterBodyData() {
        jdbcTemplate.update("delete  from fxpg_disaster_body_data");
    }

    @Override
    public void saveDisasterData(List<DisasterData> vo) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(vo.toArray());
        String sql = "INSERT INTO  fxpg_disaster_data  " +
                "(id,category,type,house_num,area) " +
                "values" +
                "(:id,:category,:type,:houseNum,:area)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void delRiskEstimateData() {
        jdbcTemplate.update("delete  from fxpg_risk_estimate_data");
    }

    @Override
    public void saveRiskEstimateData(List<RiskEstimateData> voB) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(voB.toArray());
        String sql = "INSERT INTO  fxpg_risk_estimate_data  " +
                "(id,injured,economy_loss,death,risk_value,intensity,assess_type,province,area_code) " +
                "values" +
                "(:id,:injured,:economyLoss,:death,:riskValue,:intensity,:assessType,:province,:areaCode)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveMapRiskData(List<RiskMapData> riskMapDataList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(riskMapDataList.toArray());
        String sql = "INSERT INTO  fxpg_map_risk_data  " +
                "(id,type,province,code) " +
                "values" +
                "(:id,:type,:province,:code)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveNationwideRiskData(List<NationwideRiskData> nationwideRiskDataList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(nationwideRiskDataList.toArray());
        String sql = "INSERT INTO  fxpg_nationwide_risk_data  " +
                "(id,level,number) " +
                "values" +
                "(:id,:level,:number)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveKeynoteTargetData(List<KeynoteTargetData> keynoteTargetDataList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(keynoteTargetDataList.toArray());
        String sql = "INSERT INTO  fxpg_keynote_target_data  " +
                "(id,type,level,count) " +
                "values" +
                "(:id,:type,:level,:count)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void deleteKeyNoteTargetData() {
        jdbcTemplate.update("delete from fxpg_keynote_target_data");
    }

    @Override
    public void saveKeynoteMapData(List<KeynoteData> keynoteMapDataList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(keynoteMapDataList.toArray());
        String sql = "INSERT INTO  fxpg_keynote_target_map_data  " +
                "(id," +
                "type," +
                "province_code," +
                "city_code," +
                "county_code," +
                "longitude," +
                "latitude," +
                "level) " +
                "values" +
                "(:id,:keynoteName,:provinceCode,:cityCode,:countyCode,:longitude,:latitude,:level)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveAssessQh(List<AssessQh> riskEstimateDataList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(riskEstimateDataList.toArray());
        String sql = "INSERT INTO  fxpg_qh_data  " +
                "(id," +
                "province," +
                "city," +
                "county," +
                "geom," +
                "estimate_obj," +
                "condition," +
                "qh_level," +
                "assess_type) " +
                "values" +
                "(:id,:province,:city,:county,ST_GeometryFromText(:geomText,4490),:estimateObj,:condition,:qhLevel,:assessType)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveDisasterBodyData(List<DisasterBodyData> disasterBodyDataList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(disasterBodyDataList.toArray());
        String sql = "INSERT INTO  fxpg_disaster_body_data  " +
                "(id," +
                "house_use," +
                "structure_type," +
                "geom) " +
                "values" +
                "(:id,:houseUse,:structureType,ST_GeometryFromText(:geomText,4490))";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveDisasterOverViewData(List<DisasterOverview> resultList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(resultList.toArray());
        String sql = "INSERT INTO  fxpg_disaster_overview  " +
                "(id," +
                "disaster_type," +
                "pole_high_proportion," +
                "high_proportion," +
                "centre_proportion," +
                "low_proportion," +
                "pole_low_proportion," +
                "yes_proportion," +
                "no_proportion," +
                "system) " +
                "values" +
                "(:id,:disasterType,:poleHighProportion,:highProportion,:centreProportion,:lowProportion,:poleLowProportion,:yesProportion,:noProportion,:system)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void saveVisitData(List<Visit> visitList) {
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(visitList.toArray());
        String sql = "INSERT INTO  xczh_disaster_visits_count  " +
                "(id," +
                "today_visit_num," +
                "month_visit_num," +
                "accumulate_visit_num," +
                "system,province) " +
                "values" +
                "(:id,:todayVisitNum,:monthVisitNum,:accumulateVisitNum,:system,:province)";
        namedParameterJdbcTemplate.batchUpdate(sql, beanSources);
    }

    @Override
    public void deleteVisitData() {
        jdbcTemplate.update("delete from xczh_disaster_visits_count");
    }

    @Override
    public void delDisasterOverViewData() {
        jdbcTemplate.update("delete from fxpg_disaster_overview");
    }

    @Override
    public void deleteRiskEstimateData() {
        jdbcTemplate.update("delete from fxpg_qh_data");
    }

    @Override
    public void deleteKeynoteMapData() {
        jdbcTemplate.update("delete from fxpg_keynote_target_map_data");
    }

    @Override
    public ActivityFaultData getActivityFaultData() {
        String sql = "SELECT * FROM fxpg_activity_fault_data";
        List<ActivityFaultData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(ActivityFaultData.class));
        return list.get(0);
    }

    @Override
    public List<NationwideRiskData> getNationwideRiskData() {
        String sql = "SELECT * FROM fxpg_nationwide_risk_data";
        List<NationwideRiskData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(NationwideRiskData.class));
        return list;
    }

    @Override
    public List<DisasterBodyData> getBodyData() {
        String sql = "SELECT *,ST_AsText(geom) geom FROM fxpg_disaster_body_data";
        List<DisasterBodyData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisasterBodyData.class));
        return list;
    }

    @Override
    public List<KeynoteTargetData> getKeynoteTargetData() {
        String sql = "SELECT * FROM fxpg_keynote_target_data";
        List<KeynoteTargetData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(KeynoteTargetData.class));
        return list;
    }


    @Override
    public List<RiskEstimateData> getRiskEstimateData(String estimate0bj,String condition,String assessType) {
        String sql = "SELECT * FROM fxpg_risk_estimate_data where 1=1 ";
        if(StringUtils.isNotBlank(assessType)){
            sql+=" and assess_type='"+assessType+"'";
        }
        if(StringUtils.isNotBlank(condition)){
            sql+=" and intensity='"+condition+"'";
        }

        List<RiskEstimateData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RiskEstimateData.class));
        return list;
    }

    @Override
    public List<RiskPlanData> getRiskPlanData(String assessType,String estimate0bj,String condition) {
        //String sql = "SELECT *,ST_AsText(geom) geom FROM fxpg_risk_plan_data order by cast (level as int) desc";
        String sql = "SELECT *,ST_AsText(geom) geom,qh_level as level  FROM fxpg_qh_data where estimate_obj='"+estimate0bj+"' and assess_type='"+assessType+"' ";
        if(StringUtils.isNotBlank(condition)){
            sql+=" and condition='"+condition+"' ";
        }
        sql+=" order by qh_level desc";
        List<RiskPlanData> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RiskPlanData.class));
        return list;
    }

    @Override
    public List<DisasterOverview> getDisasterOverview(String system) {
        String sql = "SELECT * FROM fxpg_disaster_overview where system = '"+system+"'";
        List<DisasterOverview> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(DisasterOverview.class));
        return list;
    }

    @Override
    public void deleteMapRiskData() {
        jdbcTemplate.update("delete from fxpg_map_risk_data");
    }

    @Override
    public void deleteNationwideRiskData() {
        jdbcTemplate.update("delete from fxpg_nationwide_risk_data");
    }

}
